// JavaScript Document
var LOADING_SMALL	='<img id="loading-small" src="../images/loading-small.gif" />';
// add a new Brand
function addBrand(){
	alert('error');
	brandName		= document.getElementById('brandName').value;
	check		= true;	

	if(check==true){
		brandName		= encodeURIComponent(brandName);
		pos			= "brand-div";
		loading		= "submit-loading"
		xmlHttp 	= GetXmlHttpObject();

		if (xmlHttp == null) {
			alert("Your browser does not support AJAX!");
			return;
		}
		var url = "../controllers/controller-admin-brand.php?action=add-brand&brandName="+brandName;
		url=url+"&sid="+Math.random();
		xmlHttp.onreadystatechange = function () {
			document.getElementById(loading).innerHTML = LOADING_SMALL;
			if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
				document.getElementById(pos).innerHTML 		= xmlHttp.responseText;
				document.getElementById(loading).innerHTML	= "";
			}
		};
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
}
function addModel(){
	//alert("error");
	brandID 		= document.getElementById('brandID').value;
	modelName		= document.getElementById('modelName').value;
	check		= true;	

	if(check==true){
		modelName		= encodeURIComponent(modelName);
		pos			= "model-div";
		loading		= "submit-loading"
		xmlHttp 	= GetXmlHttpObject();

		if (xmlHttp == null) {
			alert("Your browser does not support AJAX!");
			return;
		}
		var url = "../controllers/controller-admin-model.php?action=add-model&brandID="+brandID+"&modelName="+modelName;
		url=url+"&sid="+Math.random();
		xmlHttp.onreadystatechange = function () {
			document.getElementById(loading).innerHTML = LOADING_SMALL;
			if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
				document.getElementById(pos).innerHTML 		= xmlHttp.responseText;
				document.getElementById(loading).innerHTML	= "";
			}
		};
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
}
function printSeriesDrop(){
	//alert('1');
	modelID	=	document.getElementById('modelID').value;
	//alert('2');
	check=true;
	if(check=true){
		modelID=encodeURIComponent(modelID);
		pos="series-drop-container";
		xmlHttp=GetXmlHttpObject();
		if(xmlHttp==null){
			alert("Your browser does not support AJAX!");
			return;
		}
		
		var url="../controllers/controller-admin-series.php?action=print-series-drop&modelID="+modelID;
		url=url+"&sid="+Math.random();
		xmlHttp.onreadystatechange=function(){
			document.getElementById(pos).innerHTML=LOADING_SMALL;
			if(xmlHttp.readyState==4||xmlHttp.readyState=="complete"){
				document.getElementById(pos).innerHTML=xmlHttp.responseText;
			}
		};
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}
function printModelDrop(){
	//alert("1");
	brandID 		= document.getElementById('brandID').value;
	//alert("2");
	check		= true;	

	if(check==true){
		brandID		= encodeURIComponent(brandID);
		pos			= "model-drop-container";
		xmlHttp 	= GetXmlHttpObject();

		if (xmlHttp == null) {
			alert("Your browser does not support AJAX!");
			return;
		}
		//alert("3");
		var url = "../controllers/controller-admin-model.php?action=print-model-drop&brandID="+brandID;
		url=url+"&sid="+Math.random();
		//alert("4");
		xmlHttp.onreadystatechange = function () {
			document.getElementById(pos).innerHTML = LOADING_SMALL;
			if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
				document.getElementById(pos).innerHTML 		= xmlHttp.responseText;
			}
		};
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
}
//Series-------------------------------

function addSeries(){
	//alert("1");
	brandID 		= document.getElementById('brandID').value;
	modelID			= document.getElementById('modelID').value;
	seriesName		= document.getElementById('seriesName').value;
	check		= true;	
	//alert("2");
	if(check==true){
		seriesName		= encodeURIComponent(seriesName);
		pos			= "series-div";
		loading		= "submit-loading"
		xmlHttp 	= GetXmlHttpObject();

		if (xmlHttp == null) {
			alert("Your browser does not support AJAX!");
			return;
		}
		var url = "../controllers/controller-admin-series.php?action=add-series&modelID="+modelID+"&brandID="+brandID+"&seriesName="+seriesName;
		url=url+"&sid="+Math.random();
		xmlHttp.onreadystatechange = function () {
			document.getElementById(loading).innerHTML = LOADING_SMALL;
			if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
				document.getElementById(pos).innerHTML 		= xmlHttp.responseText;
				document.getElementById(loading).innerHTML	= "";
			}
		};
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
		//alert("2");
	}
}
// Add Product
function addProduct(){
	//alert('s');
	brandID			=	document.getElementById('brandID').value;
	modelID			=	document.getElementById('modelID').value;
	seriesID		=	document.getElementById('seriesID').value;
	image			=	document.getElementById('uploadImage2').value;
	quantity		=	document.getElementById('quantity').value;
	price			=	document.getElementById('price').value;
	
	check=true;
	if(check==true){
		image		=	encodeURIComponent(image);
		pos			=	"product-div";
		loading		=	"submit-loading";
		xmlHttp		=	GetXmlHttpObject();
		if(xmlHttp==null){
			alert("You're browser does not support AJAX!");
			return;
		}
		var url ="../controllers/controller-admin-product.php?action=add-product&brandID="+brandID+
		"&modelID="+modelID+"&seriesID="+seriesID+"&image="+image+"&quantity="+quantity+"&price="+price;
		url=url+"&sid"+Math.random();
		
		xmlHttp.onreadystatechange = function () {
			document.getElementById(loading).innerHTML = LOADING_SMALL;
			if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
				document.getElementById(pos).innerHTML 		= xmlHttp.responseText;
				document.getElementById(loading).innerHTML	= "";
			}
		};
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
		alert('3');
	}
}
// Add Product
function addProductDetail(){
	//alert('s');
	productID			=	document.getElementById('productID').value;
	screen			=	document.getElementById('screen').value;
	cpu		=	document.getElementById('cpu').value;
	ram			=	document.getElementById('ram').value;
	os		=	document.getElementById('os').value;
	sim			=	document.getElementById('sim').value;
	camera			=	document.getElementById('camera').value;
	memory_in			=	document.getElementById('memory_in').value;
	memory_out			=	document.getElementById('memory_out').value;
	cell			=	document.getElementById('cell').value;
	
	check=true;
	if(check==true){
		pos			=	"product-div";
		loading		=	"submit-loading";
		xmlHttp		=	GetXmlHttpObject();
		if(xmlHttp==null){
			alert("You're browser does not support AJAX!");
			return;
		}
		var url ="../controllers/controller-admin-product.php?action=add-product-detail&productID="+productID+
		"&screen="+screen+"&cpu="+cpu+"&ram="+ram+"&os="+os+"&sim="+sim+"&camera="+camera+"&memory_in="+memory_in+"&memory_out="+memory_out+"&cell="+cell;
		url=url+"&sid"+Math.random();
		
		xmlHttp.onreadystatechange = function () {
			document.getElementById(loading).innerHTML = LOADING_SMALL;
			if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
				document.getElementById(pos).innerHTML 		= xmlHttp.responseText;
				document.getElementById(loading).innerHTML	= "";
			}
		};
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
		alert('3');
	}
}
//Update Product
function updateProduct(){
	productID		= 	document.getElementById('productID').value;
	brandID			=	document.getElementById('brandID').value;
	modelID			=	document.getElementById('modelID').value;
	seriesID		=	document.getElementById('seriesID').value;
	image			=	document.getElementById('uploadImage2').value;
	quantity		=	document.getElementById('quantity').value;
	price			=	document.getElementById('price').value;
	alert('4');
	check=true;
	if(check==true){
		image		=	encodeURIComponent(image);
		pos			=	"product-div";
		loading		=	"submit-loading";
		xmlHttp		=	GetXmlHttpObject();
		if(xmlHttp==null){
			alert("You're browser does not support AJAX!");
			return;
		}
		var url ="../controllers/controller-admin-product.php?action=update-product&productID="+productID+"&brandID="+brandID+
		"&modelID="+modelID+"&seriesID="+seriesID+"&image="+image+"&quantity="+quantity+"&price="+price;
		url=url+"&sid"+Math.random();
		
		xmlHttp.onreadystatechange = function () {
			document.getElementById(loading).innerHTML = LOADING_SMALL;
			if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
				document.getElementById(pos).innerHTML 		= xmlHttp.responseText;
				document.getElementById(loading).innerHTML	= "";
			}
		};
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
		alert('3');
	}
}
//-------------------------------
var xmlHttp;
function GetXmlHttpObject() {
	var xmlHttp = null;
	try {
	  // Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e) {
	  // Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
